package se.webbzon.boltzmann.game.event;

import se.webbzon.boltzmann.game.object.player.AbstractPlayerCharacter;
import se.webbzon.boltzmann.game.sequence.Sequence;
import se.webbzon.boltzmann.game.sequence.Sequences;
import se.webbzon.boltzmann.game.sequence.builder.AbstractSequenceBinding;
import se.webbzon.boltzmann.inventory.Inventory;
import se.webbzon.boltzmann.item.AbstractItem;
import se.webbzon.boltzmann.item.ItemGroup;
import se.webbzon.oschi01.worldarea.WorldArea;
import se.webbzon.oschi01.worldobject.WorldObject;

public class GiveItemEvent extends AbstractGameEvent {
	
	// The sequence associated with this game event
	private final Sequence sequence;

	/*============================================================
	Constructors
	============================================================*/

	/** Gives the current player the item with the id argument[0]. **/
	public GiveItemEvent(String[] arguments, Sequence sequence) {
		super(arguments);
		this.sequence = sequence;
	}
	
	/*============================================================
	Public Methods
	============================================================*/

	@Override protected void onBegin(WorldArea world) {
		if (arguments() > 0) {
			final int itemId = (int) 
					((double) Sequences.parse(sequence, getArgument(0)));

			// Retrieve the inventory of the player
			final WorldObject wso = 
					sequence.getBinding(AbstractSequenceBinding.PLAYER);
			final Inventory inv = 
					((AbstractPlayerCharacter) wso).getPlayer().getInventory();

			AbstractItem item = ItemGroup.instance().getItem(itemId);
			if (item != null)
				inv.addItem((AbstractItem) item.clone());
			else
				exception("The item with the id " + itemId + " was not found.");
		} else
			exception("Argument exception.");
		end();
	}

	@Override protected void onEnd() {};

}
